using DevExpress.Utils;
using DevExpress.XtraEditors.Mask;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;

namespace UI.Helper
{
    /// <summary>
    /// Respresents a precision setting hepler object.
    /// </summary>
    public class PrecisionHelper
    {
        /// <summary>
        /// Converts display format column.
        /// </summary>
        public void FormatCurencyPrecision(params GridColumn[] columns)
        {
            // format string
            //var maskStringFormat = string.Format("{{0:n{0}}}", 0);
            //const string maskStringFormat = "{0:0;(0)}";
            //const string maskStringFormat = "n0";
            //var maskStringFormat = string.Format("{{0:n{0}}}", 0);
            const string maskStringFormat = "n0";

            // apply format to each grid column
            foreach (var column in columns)
            {
                column.DisplayFormat.FormatString = maskStringFormat;
                column.DisplayFormat.FormatType = FormatType.Custom;
                if (column.ColumnEdit != null)
                {
                    column.ColumnEdit.EditFormat.FormatString = maskStringFormat;
                }
                if (column.SummaryItem.Collection.ActiveCount > 0)
                {
                    column.SummaryItem.DisplayFormat = "{0:n0}";
                }
            }
        }

        /// <summary>
        /// Converts display format spin edit.
        /// </summary>
        public void FormatCurencyPrecision(params RepositoryItemSpinEdit[] spins)
        {
            // format string
            var maskStringFormat = "\\d{0,9}(\\.\\d{0," + 0 + "})?";

            // apply format to each spin edit
            foreach (var spin in spins)
            {
                spin.Mask.EditMask = maskStringFormat;
                spin.Mask.MaskType = MaskType.Numeric;
            }
        }
    }
}
